<?php

/*
 * UsersTransaction Controller
 * created : 07/02/2013
 * author : Nguyen Van Nhieu <nhieunv@evolable.asia>
 * 
 */

class UserTransactionsController extends AppController{
	
	var $uses = '';
	public function beforeFilter() {
		parent::beforeFilter();
		//$this->CustomAuth->allow();
	}
	
	/**
	 * default action
	 * @purpose : list && filter all users of websites.
	 * @param : ?
	 */
	function admin_index() {
		if(isset($this->params['url']['type']) && $this->params['url']['type'] === 'Course'){
			$this->redirect(array('controller' => 'courseTransaction', 'action' => 'admin_index'));
		}
		$this->loadModel('Profile');
		$today = date('Y-m-d H:i:s');
		$options['fields'] = array(
			'distinct(Profile.user_id)', 'Profile.first_name', 'Profile.last_name', 'Profile.id','Profile.icon_image','Profile.del_flg', 'Profile.headline',
			'EnteredCounting.entered_number', 'AttendCounting.ettend_number', 'RecommendTeacher.id'
		);
		$options['joins'] = array(
			array(
				'table' => "(Select count(ec.user_id) as entered_number, ec.user_id 
								From entered_courses ec, courses_detail cd 
								where cd.opening_date <='".$today."' and ec.del_flg=0 and cd.del_flg=0 and ec.detail_id = cd.id 
								group by user_id ) ",
				'alias' => 'EnteredCounting',
				'type' => 'left',
				'conditions' => array(
					'Profile.user_id = EnteredCounting.user_id'
				)
			),
			array(
				'table' => "(Select count(ec.user_id) as ettend_number, ec.user_id 
								From entered_courses ec, courses_detail cd 
								where cd.opening_date >'".$today."' and ec.del_flg=0 and cd.del_flg=0 and ec.detail_id = cd.id 
								group by user_id )",
				'alias' => 'AttendCounting',
				'type' => 'left',
				'conditions' => array(
					'Profile.user_id = AttendCounting.user_id'
				)
			),
			array(
				'table' => 'recommend_teachers',
				'alias' => 'RecommendTeacher',
				'type' => 'left',
				'conditions' => array(
					'Profile.user_id = RecommendTeacher.user_id'
				)
			)
			
		);
		if(isset($this->params['url']['limit'])){
			$options['limit'] = intval($this->params['url']['limit']);
		}else{
			$options['limit'] = 25;
		}		
		$status = 0;
		if(isset($this->params['url']['status']) && in_array($this->params['url']['status'], array('0','1'))){
			$status = intval($this->params['url']['status']);
		}
		$options['conditions'] = array(
			'Profile.del_flg' => $status
		);
		if(isset($this->params['url']['name']) && !empty($this->params['url']['name'])){
			array_push($options['conditions'], "Concat(Profile.last_name,' ',Profile.first_name ) like '%".htmlspecialchars($this->params['url']['name'])."%'");
		}
		$this->paginate = $options;
		$users = $this->paginate('Profile');
		$this->set(array('users' => $users));
	}
	
	/**
	 * action view user's transaction
	 * @purpose view list transaction of user
	 * @param $userId - user id
	 */
	function admin_view($userId) {
		if(!(isset($userId))){
			$this->cakeError('error404');
		}
		$this->loadModel('Transaction');
		$this->loadModel('Profile');
		$userId = intval($userId);
		$options['fields'] = array(
			'Transaction.*', 'PaymentMethod.payment_method'
		);
		
		$options['conditions'] = array(
			'Transaction.user_id' => $userId,
		);
		if(isset($this->params['url']['payment']) && in_array($this->params['url']['payment'], array('0','1'))){
			array_push($options['conditions'], 'Transaction.status = '.intval($this->params['url']['payment']));
		}
		
		if(isset($this->params['url']['delete']) && in_array($this->params['url']['delete'], array('0', '1'))){
			array_push($options['conditions'], 'Transaction.del_flg = '.intval($this->params['url']['delete']));
		}
		$this->paginate = $options;
		$transactions = $this->paginate('Transaction');
		//get profile
		$profile = $this->Profile->find('first', array(
													'fields' => array($this->Profile->alias.'.first_name', $this->Profile->alias.'.last_name', $this->Profile->alias.'.headline', $this->Profile->alias.'.icon_image', $this->Profile->alias.'.id'),
													'conditions' => array('Profile.user_id' => $userId)));
		$enteredClassNumber = $this->_getEnteredClassNumber($userId);
		$attendClassNumber = $this->_getAttendClassNumber($userId);
		$this->set(array('transactions' => $transactions, 'profile' => $profile, 'enteredClassNumber' => $enteredClassNumber, 'attendClassNumber' => $attendClassNumber));
	}
}
?>
